<template>
  <div>
    <div class="login-all">
      <div class="login-title">您好!</div>
      <div class="login-text">欢迎来到职业帮,立即注册</div>
      <div class="login-form">
        <label>
          <div class="login-address">+86</div>
          <input
            class="phone"
            type="text"
            placeholder="请输入手机号"
            v-model="phone"
          />
        </label>
        <label>
          <input
            class="verif"
            type="text"
            placeholder="请输入验证码"
            v-model="code"
          />
        </label>
        <div
          class="verification"
          ref="verification"
          @click.stop="sendVerfHandler"
        >
          发送验证码
        </div>
      </div>
      <div class="login-btn" @click.stop="loginHandler">登录</div>
      <div class="login-little">
        <div class="login-line"></div>
        <div class="login-other">其他登录方式</div>
      </div>
      <div class="login-img">
        <img src="@/../public/images/1668395599815.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  name: "LoginView",
  data: () => {
    return {
      timer: null,
      phone: "13612254690",
      code: "1234",
      verification: "1234",
      intercept: false,
    };
  },
  methods: {
    //登陆请求
    async loginHandler() {
      let ptt = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
      if (
        !this.phone ||
        !this.code ||
        !ptt.test(this.phone) ||
        this.verification != this.code
      ) {
        this.$toast("输入错误");
        return;
      }
      let res = await this.axios({
        method: "post",
        url: "/user/phone-login",
        data: qs.stringify({
          phone: this.phone,
          code: this.code,
        }),
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
      });
      if (!(res?.data?.statusCode == 200)) {
        this.$toast("系统繁忙");
        return;
      }
      localStorage.setItem("token", res.data.data.token);
      this.$store.commit("setUser", { user: res.data.data.userInfo });
      this.$router.replace("/message");
    },
    //发送验证码
    async sendVerfHandler() {
      let ptt = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
      if (!ptt.test(this.phone)) {
        this.$toast("手机号错误！");
        return;
      }
      if (this.intercept) return;
      this.intercept = true;
      let index = 60;
      this.timer = setInterval(() => {
        if (--index == -1) {
          this.$refs.verification.textContent = `发送验证码`;
          this.intercept = false;
          clearInterval(this.timer);
          return;
        }
        this.$refs.verification.textContent = `${index}s后重新发送`;
      }, 1000);
      let res = await this.axios.post("/user/phone-get-code");
      if (res.data.statusCode == 200) {
        this.verification = res.data.data.code;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.login-all {
  width: 100%;
  box-sizing: border-box;
  padding: 1rem;
  .login-title {
    font-size: 0.48rem;
    font-weight: bold;
    color: #181818;
    margin-bottom: 0.28rem;
  }
  .login-text {
    font-size: 0.36rem;
    font-weight: 300;
    color: #333333;
  }
  .login-form {
    position: relative;
    width: 100%;
    font-size: 0.28rem;
    font-weight: 400;
    color: #333333;
    > label {
      display: flex;
      border-bottom: 0.01rem solid#ccc;
      padding: 0.3rem 0;
      .login-address {
        margin-right: 0.45rem;
      }
      > input {
        border: none;
      }
    }
    .verification {
      position: absolute;
      right: 0;
      bottom: 0.12rem;
      // width: 1.5rem;
      height: 0.5rem;
      background: #ffffff;
      border: 0.02rem solid #149639;
      border-radius: 0.35rem;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 0.24rem;
      color: #149639;
      padding: 0.1rem 0.17rem;
    }
  }
  .login-btn {
    margin: 0 auto;
    margin-top: 2.34rem;
    width: 5rem;
    height: 0.88rem;
    box-sizing: border-box;
    background: #149639;
    border-radius: 0.44rem;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.3rem;
    color: #ffffff;
  }
  .login-little {
    position: relative;
    width: 100%;
    margin: 0 auto;
    margin-top: 1.05rem;
    display: flex;
    justify-content: center;
    align-items: center;
    .login-line {
      width: 100%;
      height: 0.01rem;
      background-color: #ccc;
    }
    .login-other {
      position: absolute;
      padding: 0 0.5rem;
      background-color: #fff;
    }
  }
  .login-img {
    margin: 0 auto;
    margin-top: 0.5rem;
    width: 0.7rem;
    height: 0.57rem;
  }
}
</style>